#![feature(offset_of)]

use raw_list::{define_raw_list_head, RawListNode};

struct ListTest1 {
    value: u64,
    node: RawListNode<ListTest1>,
}

impl ListTest1 {
    fn new(value: u64) -> Self {
        Self {
            value,
            node: RawListNode::new(),
        }
    }
}

#[test]
fn list_test() {
    define_raw_list_head!(head, ListTest1, node);

    unsafe {
        assert_eq!(head.list_empty(), true);
        assert_eq!(head.list_empty_careful(), true);
    }

    let mut v = vec![];
    for i in 0..100 {
        v.push(ListTest1::new(i));
    }

    unsafe {
        for i in &mut v {
            head.list_add(&mut i.node);
        }

        let mut iter = head.iter();
        for i in (0..100).rev() {
            assert_eq!(i, iter.next().unwrap().value);
        }

        iter = head.iter();
        for i in 0..100 {
            assert_eq!(i, iter.next_back().unwrap().value);
        }

        for i in head.iter_mut() {
            if i.value == 50 {
                i.node.list_del();
                break;
            }
        }

        iter = head.iter();
        for i in 0..100 {
            if i == 50 {
                continue;
            }
            assert_eq!(i, iter.next_back().unwrap().value);
        }
    }
}
